package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0739.java
 * @Author : WangRuoyu
 * @Date 2023/11/3 22:35
 */


public class Q0739 {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] res = new int[temperatures.length];
        List<Integer> stack = new ArrayList<>();
        for (int i = temperatures.length - 1; i >= 0; i--) {
            while (!stack.isEmpty() && temperatures[stack.get(stack.size() - 1)] <= temperatures[i]) {
                stack.remove(stack.size() - 1);
            }
            if (!stack.isEmpty()) {
                res[i] = stack.get(stack.size() - 1) - i;
            }
            stack.add(i);
        }
        return res;
    }

    public static void main(String[] args) {
        Q0739 sl = new Q0739();
        int[] temperatures = {89, 62, 70, 58, 47, 47, 46, 76, 100, 70};
        System.out.println(java.util.Arrays.toString(sl.dailyTemperatures(temperatures)));
    }
}